package com.hyou.starter.autoconfigure.mybatis;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.hyou.starter.autoconfigure.datasource.MyDataSource;

/**
 * Mybatis相关的Bean自动注入配置类
 * 
 * @author FengChangshuo
 * @version 1.0.0 2018-03-22 14:10:15 初始创建
 */
@Configuration
@ConditionalOnClass(SqlSessionFactory.class)
@ConditionalOnProperty(prefix = "hyou", name = "mybatis", matchIfMissing = true)
public class MybatisAutoConfiguration {
    
    private static final Logger log = LoggerFactory.getLogger(MybatisAutoConfiguration.class);
    
    /**
     * <pre>
     * Example:
     * application.yml:
     * 
     * hyou:
     *   mybatis:
     *     mybatis-config: 'classpath:mybatis-config.xml'
     *     resource-patterns:
     *       - 'classpath*:mappers/dir1/*Mapper.xml'
     *       - 'classpath*:mappers/dir2/*Mapper.xml'
     *     mybatis-map:
     *       dskey1:
     *         mybatis-config: 'classpath:mybatis-config.xml'
     *         resource-patterns:
     *           - 'classpath*:mappers/dir3/*Mapper.xml'
     *           - 'classpath*:mappers/dir4/*Mapper.xml'
     *       dskey2:
     *         mybatis-config: 'classpath:mybatis-config.xml'
     *         resource-patterns:
     *           - 'classpath*:mappers/dir3/*Mapper.xml'
     *           - 'classpath*:mappers/dir4/*Mapper.xml'
     * </pre>
     * 
     * @return Mybatis资源配置参数对象
     */
    @Bean
    @ConfigurationProperties(prefix = "hyou.mybatis")
    public MybatisResourceProp mybatisResourceProp() {
        return new MybatisResourceProp();
    }
    
    @Bean
    @ConditionalOnMissingBean(name = "sqlSessionMap")
    public Map<String, SqlSession> sqlSessionMap(MyDataSource configDataSource) throws Exception {
        SqlSessionFactory factory;
        MybatisResourceProp mybatisResourceProp = mybatisResourceProp();
        
        Map<String, SqlSession> sqlSessionMap = new HashMap<>();
        for (Entry<String, DataSource> entry : configDataSource.getDataSourceMap().entrySet()) {
            log.debug("create datasource {} SqlSessionFactory ", entry.getKey());
            factory = MybatisUtil.sqlSessionFactory(entry.getKey(), entry.getValue(), mybatisResourceProp);
            sqlSessionMap.put(entry.getKey(), new SqlSessionTemplate(factory));
        }
        return sqlSessionMap;
    }
    
}
